//
//  SelectConditionTableView.m
//  BocaiMall
//
//  Created by 李 on 2018/9/26.
//  Copyright © 2018年 菠菜包. All rights reserved.
//

#import "SelectConditionTableView.h"
#import "ChoseTableCell.h"
@interface SelectConditionTableView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UIView *bottomBgView;
@property(nonatomic,strong)UILabel *choseTitle;
@property (nonatomic,strong) UIButton *cancleBtn;
@property(nonatomic,strong)UILabel *lineLable;
@property(nonatomic,strong)NSArray *conditionArray;
@property(nonatomic,copy)NSString *tableViewTitle;

@end
@implementation SelectConditionTableView
-(SelectConditionTableView *)initWithFrame:(CGRect)frame withConditionArray:(NSArray *)array withSelectedTitle:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.frame = frame;
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height)];
        _bgView.alpha = 0.5;
        _bgView.userInteractionEnabled = YES;
        _bgView.backgroundColor = [UIColor colorWithHexString:@"#595959"];
        [self addSubview:self.bgView];
        
        _bottomBgView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-330, SCREEN_WIDTH, 330)];
        _bottomBgView.backgroundColor = [UIColor whiteColor];
        _bottomBgView.userInteractionEnabled = YES;
        [self addSubview:self.bottomBgView];
       
        
        _choseTitle = [[UILabel alloc] init];
        _choseTitle.font = [UIFont systemFontOfSize:15.0f];
        _choseTitle.frame = CGRectMake(0, 0, SCREEN_WIDTH, 55);
        _choseTitle.text = title;
        _choseTitle.textAlignment = NSTextAlignmentCenter;
        _choseTitle.textColor = [UIColor darkGrayColor] ;
         [self.bottomBgView addSubview:self.choseTitle];
        
        _lineLable = [[UILabel alloc] init];
        _lineLable.frame = CGRectMake(0, 54, SCREEN_WIDTH, 0.5);
        _lineLable.backgroundColor =RGBCOLOR(229, 229, 229);
        [self.bottomBgView addSubview:self.lineLable];

        _cancleBtn = [[UIButton alloc] init];
        _cancleBtn.frame = CGRectMake(SCREEN_WIDTH-45, 10, 30, 30);
        [_cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancleBtn setTitleColor:[UIColor colorWithHexString:@"#2BBA98"] forState:UIControlStateNormal];
        _cancleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_cancleBtn addTarget:self action:@selector(cancleButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomBgView addSubview:self.cancleBtn];

        
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 55, SCREEN_WIDTH, 330-55) style:UITableViewStylePlain];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.showsVerticalScrollIndicator = NO;
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.scrollEnabled = YES;
        
        [_tableview registerNib:[UINib nibWithNibName:NSStringFromClass([ChoseTableCell class]) bundle:nil] forCellReuseIdentifier:@"ChoseTableCell"];
        [self.bottomBgView addSubview:self.tableview];
        [self.tableview reloadData];
        
        self.conditionArray = array;
        self.tableViewTitle = title;
        [self.tableview reloadData];
        
        
    }
    return self;
}
-(void)cancleButtonClick
{
    [self removeFromSuperview];
}
-(void)reloadDataWithConditionArray:(NSArray *)array
{
    self.conditionArray = array;
    [self.tableview reloadData];
}

#pragma mark UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        return self.conditionArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        
        ChoseTableCell *choseCell = [tableView dequeueReusableCellWithIdentifier:@"ChoseTableCell"];
        choseCell.selectionStyle =  UITableViewCellSelectionStyleNone;
        //选择状态
    
        choseCell.choseLable.text  = self.conditionArray[indexPath.row];
        return choseCell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectedCondition) {
        self.selectedCondition(self.conditionArray[indexPath.row]);
    }
}
@end
